/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.Iterator;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.tools.DragEditPartsTracker;

import edu.pku.sei.modeler.editpolicies.LifelineLayoutEditPolicy;
import edu.pku.sei.modeler.editpolicies.SequenceNodeEditPolicy;
import edu.pku.sei.modeler.figures.LifelineFigure;
import edu.pku.sei.modeler.figures.anchor.LifelineSourceAnchor;
import edu.pku.sei.modeler.figures.anchor.LifelineTargetAnchor;
import edu.pku.sei.modeler.figures.anchor.MessageAnchor;
import edu.pku.sei.modeler.figures.anchor.MessageSourceAnchor;
import edu.pku.sei.modeler.figures.anchor.MessageTargetAnchor;
import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.uml.BasicInteractions.Lifeline;
import edu.pku.sei.modeler.model.uml.Dependencies.Dependency;
import edu.pku.sei.modeler.model.uml.Kernel.Comment;

/**
 * @generated
 * 
 */
public class LifelineEditpart extends AbstractAnchoringNodeEditPart {
	public LifelineEditpart(MetaShapeElement model) {
		super(model);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		return new LifelineFigure();
	}

	protected DirectEditPolicy createDirectEditPolicy() {
		return null;
	}

	@Override
	protected IFigure getDirectEditFigure() {
		return null;
	}
	
	
	@Override
	public DragTracker getDragTracker(Request request) {
		return new DragEditPartsTracker(this) {
			@Override
			protected void updateTargetRequest() {
				super.updateTargetRequest();
				ChangeBoundsRequest request = (ChangeBoundsRequest)getTargetRequest();
				//request.getMoveDelta().y = 0;
				int height = getFigure().getBounds().y + request.getMoveDelta().y;
				if (height < 70)
					request.getMoveDelta().y = 35 - getFigure().getBounds().y;
			}
			protected Command getCommand() {
				CompoundCommand command = new CompoundCommand();
				command.setDebugLabel("Drag Object Tracker");
				Iterator iter = getOperationSet().iterator();
				Request request = getTargetRequest();
				request.setType(REQ_MOVE);
				while (iter.hasNext()) {
					EditPart editPart = (EditPart)iter.next();
					if (editPart instanceof LifelineEditpart)
						command.add(editPart.getCommand(request));
				}
				return command;
			}
		};
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new SequenceNodeEditPolicy());
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new LifelineLayoutEditPolicy());

		
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		refreshVisuals();
		super.handlePropertyChanged(event);
	}

	@Override
	public IFigure getContentPane() {
		return ((LifelineFigure)this.getFigure()).getContentPane();
//		return super.getContentPane();
	}
	
	private Lifeline getModelElement() {
		return (Lifeline) ((MetaNode) getModel()).getModelElement();
	}

	protected void refreshVisuals() {
		Lifeline modelEle = (Lifeline) getNode().getModelElement();
		LifelineFigure figure = (LifelineFigure) getFigure();
		figure.getTextLabel().setText(modelEle.getName());
		super.refreshVisuals();
	}
	
	public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart connection) {
		ConnectionAnchor anchor = super.getSourceConnectionAnchor(connection);
		if (anchor != null && anchor instanceof MessageSourceAnchor) {
			LifelineSourceAnchor lifelineAnchor = new LifelineSourceAnchor(this.getFigure());
			lifelineAnchor.setOffset(((MessageAnchor)anchor).getOffset());
			lifelineAnchor.setDelay(((MessageAnchor)anchor).getDelay());
			return lifelineAnchor;
		}
		return null;
	}

	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		
		ConnectionAnchor anchor = super.getSourceConnectionAnchor(request);
		if (anchor != null && anchor instanceof MessageSourceAnchor) {
			LifelineSourceAnchor lifelineAnchor = new LifelineSourceAnchor(this.getFigure());
			lifelineAnchor.setOffset(((MessageAnchor)anchor).getOffset());
			lifelineAnchor.setDelay(((MessageAnchor)anchor).getDelay());
			return lifelineAnchor;
		}
		return null;
	}

	public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart connection) {
		if (connection != null && connection.getSource() != null &&
				((MetaNode)connection.getSource().getModel()).getModelElement() instanceof Comment) {
			return super.getTargetConnectionAnchor(connection);
		} // this is to link the comment
		ConnectionAnchor anchor = super.getTargetConnectionAnchor(connection);
		if (anchor != null && anchor instanceof MessageTargetAnchor) {
			LifelineTargetAnchor lifelineAnchor = new LifelineTargetAnchor(this.getFigure());
			lifelineAnchor.setOffset(((MessageAnchor)anchor).getOffset());
			lifelineAnchor.setDelay(((MessageAnchor)anchor).getDelay());
			return lifelineAnchor;
		}
		return null;
	}

	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		ConnectionAnchor anchor = super.getTargetConnectionAnchor(request);
		if (anchor != null && anchor instanceof MessageTargetAnchor) {
			LifelineTargetAnchor lifelineAnchor = new LifelineTargetAnchor(this.getFigure());
			lifelineAnchor.setOffset(((MessageAnchor)anchor).getOffset());
			lifelineAnchor.setDelay(((MessageAnchor)anchor).getDelay());
			return lifelineAnchor;
		}
		return null;
	}
}
